package com.connection;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataConnection {
	public DBHelp help;
	public static SQLiteDatabase db;
	public Context c;

	public DataConnection(Context c) {
		this.c = c;
	}

	public DataConnection open() {
		help = new DBHelp(c);
		db = help.getWritableDatabase();
		db = help.getReadableDatabase();
		return this;
	}

	public void close() {
		help.close();
	}
	
//user info
	public void addUserInfo(String name, String password, String email, String phone) {
		ContentValues values = new ContentValues();
		values.put("userName", name);
		values.put("password", password);
		values.put("phone", email);
		values.put("address", phone);
		db.insert("User", null, values);
	}
	
	public Cursor queryUserInfo() {
		db = help.getReadableDatabase();
		Cursor c = db.query("User", null, null, null, null, null, null);
		return c;
	}
	
	public void deleteUserInfo(long nbr) {
		if (db == null) {
			db = help.getWritableDatabase();

		} else {
			int id = (int) nbr;
			db.delete("User", "_id=?", new String[]{String.valueOf(id)});
		}
	}
//product info	
	public void addProductInfo(String product_name, String product_price, String photo, String product_introduction){
		ContentValues values = new ContentValues();
		values.put("productName", product_name);
		values.put("price", product_price);
		values.put("picture", photo);
		values.put("introduction", product_introduction);
		db.insert("Product", null, values);
	}
	
	public Cursor queryProductInfo(){
		db = help.getReadableDatabase();
		Cursor c = db.query("Product", null, null, null, null, null, null);
		return c;
	}
	
	public void deleteProductInfo(long nbr) {
		if (db == null) {
			db = help.getWritableDatabase();

		} else {
			int id = (int) nbr;
			db.delete("Product", "_id=?", new String[]{String.valueOf(id)});
		}
	}
	
	public void deleteListInfo(long nbr) {
		if (db == null) {
			db = help.getWritableDatabase();

		} else {
			int id = (int) nbr;
			db.delete("List", "_id=?", new String[]{String.valueOf(id)});
		}
	}
	
//order info	
	public Cursor queryListInfo(){
		db = help.getReadableDatabase();
		Cursor c = db.query("List", null, null, null, null, null, null);
		return c;
	}
	
	
	
	public void addOrderInfo(String user_name, String product_name, String amount, String price){
		ContentValues values = new ContentValues();
		values.put("amount", amount);
		values.put("uname", user_name);
		values.put("pname", product_name);
		values.put("pprice", price);
		db.insert("List", null, values);
	}

}
